package com.example.android2019.note;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.android2019.R;

import java.util.ArrayList;

/**
 * 首页
 */
public class NoteActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    ListView listView;
    ImageView image_add;

    NoteBean noteBean;
    NoteAdapter noteAdapter;
    NoteSQliteHelper noteSQliteHelper;
    ArrayList<NoteBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        listView = findViewById(R.id.note_listview);
        image_add = findViewById(R.id.note_image_add);

        /*点击事件：跳转*/
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NoteActivity.this, RecordActivity.class);

                startActivityForResult(intent, 1);
            }
        };
        /*图片绑定事件*/
        image_add.setOnClickListener(listener);


        /*获取传递的数据*/
        Intent intent = this.getIntent();
        String a = intent.getStringExtra("test");
        Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            /* listView 数据的展示 方法*/
            showDate();
        }
    }

    private void showDate() {
        noteSQliteHelper = new NoteSQliteHelper(NoteActivity.this);
        list = noteSQliteHelper.query();
        noteAdapter = new NoteAdapter(NoteActivity.this, list);
        listView.setAdapter(noteAdapter);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /*获取当前点击的列表项*/
        noteBean = (NoteBean) listView.getItemAtPosition(position);
        Intent intent = new Intent(NoteActivity.this, RecordActivity.class);
        intent.putExtra("id", noteBean.getId());
        intent.putExtra("content", noteBean.getContent());
        intent.putExtra("time", noteBean.getTime());
        startActivityForResult(intent, 1);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(NoteActivity.this);
        dialog.setTitle(" 删除");
        dialog.setMessage("确定要删除此事件吗?");
        /* dialog.setPositiveButton("确定",listView);*/
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                noteBean = list.get(position);
                if (noteSQliteHelper.deleteDate(noteBean.getId())) {
                    list.remove(position);
                    noteAdapter.notifyDataSetChanged();
                    Toast.makeText(NoteActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.setNegativeButton("取消", null);
        dialog.show();
        return true;
    }
}
